﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class AnimatorEventHandler : MonoBehaviour
{
    private Transform CreateEffect( string path, Transform parent = null )
    {
        Transform e = (GameObject.Instantiate(Resources.Load(string.Format("Prefab/Effect/{0}", path), typeof(GameObject))) as GameObject).transform;
        e.parent = parent;
        e.localPosition = Vector3.zero;
        e.localEulerAngles = Vector3.zero;
        e.localScale = Vector3.one;
        return e;
    }

    ///<summary>
    /// "this.Parent"에 이펙트 오브젝트를 생성.
    ///</summary>
    ///<param name="sParam"></param>
    public void ShowEffect( string sParam )
    {
        Transform effect = CreateEffect(sParam, transform.parent);
    }

    /// <summary>
    /// "World"에 이펙트 오브젝트를 생성.
    /// "this.Parent"의 위치, 회전, 크기값을 가짐.
    /// </summary>
    /// <param name="sParam"></param>
    public void ShowEffectPos( string sParam )
    {
        Transform effect = CreateEffect(sParam);
        Transform parent = transform.parent;
        effect.position = parent.position;
        effect.rotation = parent.rotation;
        effect.localScale = parent.localScale;
    }

    /// <summary>
    /// "this.Parent의 Pivot"에 이펙트 오브젝트 생성.
    /// fileNmae : "Pivot,effectName"
    /// </summary>
    /// <param name="sParam"></param>
    public void ShowEffectFollowPivot( string sParam )
    {
        string[] args = sParam.Split(',');
        Transform effect = CreateEffect(args[0], MyUtil.GetChild(transform, args[1]));
    }

    //public void ShowEffectClass( string sParam )
    //{
    //    SectionBattleMember sectionM = transform.parent.GetComponent<SectionBattleMember>();
    //    string strAttacker = (sectionM.attacker != null ? (int) sectionM.attacker.unitInfo.cls : (int) sectionM.unitInfo.cls).ToString();
    //    Debug.Log(sectionM.attacker.unitInfo.cls + " " + sectionM.unitInfo.cls);
    //    Transform effect = CreateEffect(sParam + strAttacker, transform.parent);
    //}

    //public void ShowEffectTarget( string sParam )
    //{
    //    Transform effect;
    //    foreach(SectionBattleMember mem in SectionBattleQueue.Inst.Attacker.Targets)
    //        effect = CreateEffect(sParam, mem.transform);
    //}

    //public void ShowEffectRange( string sParam )
    //{
    //    List<Vector3> points = new List<Vector3>();
    //    foreach(SectionBattleMember mem in SectionBattleQueue.Inst.Attacker.Targets)
    //        points.Add(mem.transform.localPosition);
    //    Vector3 cp = Util.GetCenterPoint(points.ToArray());

    //    Transform effect = CreateEffect(sParam);
    //    effect.localPosition = cp;
    //    effect.localEulerAngles = Vector3.zero;
    //    effect.localScale = Vector3.one;
    //}
}
